package com.frxuexi.mdm2207.view

import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarDefaults
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.NavigationBarItemDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.frxuexi.apps.AppsPage
import com.frxuexi.lib_base.R


import com.frxuexi.mdm2207.demo.Demo05
import com.frxuexi.mymvi.AppDetailPage
import com.frxuexi.mymvi.AppsData
import com.frxuexi.mymvi.MyAppPage

sealed class NavItem(val route: String, val title: String, val img: Int) {
    object Message : NavItem("message", "消息", R.drawable.icon1275)
    object Home : NavItem("home", "门户", R.drawable.icon64)
    object Mine : NavItem("mine", "我", R.drawable.icon1405)
}

@Composable
fun MainPage() {
    val navHostController = rememberNavController()
    val list = listOf<NavItem>(NavItem.Message, NavItem.Home, NavItem.Mine)
    //获取当前返回堆栈实体状态
    val stackEntryState by navHostController.currentBackStackEntryAsState()
    //获取路径信息
    val destination = stackEntryState?.destination
    Scaffold(
        bottomBar = {
            //解决二级页面不用显示底部导航
            if (list.map { it.route }.contains(destination?.route) == true) {
                NavigationBar {
                    list.forEach { nav ->
                        NavigationBarItem(
                            colors = NavigationBarItemDefaults.colors(
                                selectedIconColor= Color.Red,selectedTextColor= Color.Red,
                                indicatorColor= NavigationBarDefaults.containerColor)
                            ,selected = destination?.hierarchy?.any { it.route == nav.route } == true,
                            onClick = {
                                navHostController.navigate(nav.route) {
                                    //栈顶复用
                                    popUpTo(navHostController.graph.startDestinationId) {
                                        //保存状态
                                        saveState = true
                                    }
                                    launchSingleTop = true
                                    //恢复状态
                                    restoreState = true
                                }
                            },
                            icon = {
                                Icon(
                                    painter = painterResource(id = nav.img),
                                    contentDescription = "",
                                    modifier = Modifier.size(30.dp)
                                )
                            },
                            label = {
                                Text(text = nav.title)
                            })
                    }
                }
            }
        }
    ) {
        NavHost(
            modifier = Modifier.padding(it),
            navController = navHostController,
            startDestination = "home",
            builder = {
                composable("message") { MessagePage() }
                composable("home") { HomePage(navHostController) }
                composable("mine") { MinePage() }
//                composable("demo05") { Demo05(navController = navHostController) }
//                composable("apps"){ AppsPage(navHostController) }
                composable("apps"){ MyAppPage(navHostController) }
                composable("detail"){
//                    val json = it.arguments?.getSerializable("det")/*.getString("det", "")*/ as AppsData
                    AppDetailPage() }
            })
    }
}